﻿Public Class frmGrammar
    Dim mygrammar As New Maaharaa.dvGrammar()
    Dim GrammarPatternList As New Dictionary(Of String, String)
    Dim ignorelist As New List(Of String)
    Dim temGrammarError As String
    Dim myfont1 As New Font("MV Boli", 10)
    Dim counter As Integer = 1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim pos As Integer = 0
        Dim searchtext As String = temGrammarError
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
                rtbInput.SelectedText = txtSuggestion.Text
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
            Exit Do
        Loop
        CheckError()
    End Sub

    Private Sub CheckError()
        GrammarPatternList = mygrammar.returnlist
        txtExplain.Clear()
        txtSuggestion.Clear()
        
        For Each item In GrammarPatternList
            Dim pos As Integer = 0
            If ignorelist.Contains(item.Key) = False Then
                Dim searchtext As String = item.Key
                pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
                Do While pos <> -1
                    Dim myfont As New Font("Faruma", 11, FontStyle.Bold)
                    If rtbInput.SelectedText = searchtext Then
                        temGrammarError = searchtext
                        rtbInput.SelectionLength = searchtext.Length
                        rtbInput.SelectionColor = Color.Green
                        rtbInput.SelectionUnderlineStyle = ExtendedRichTextBox.UnderlineStyle.HeavyWave
                        Dim mystring As String() = item.Value.Split(":")
                        txtSuggestion.Text = mystring(0)
                        txtExplain.Text = mystring(1)
                        Exit For
                    End If
                    pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
                Loop
            End If
        Next

    End Sub

    Private Sub frmGrammar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rtbInput.Text = frmMain.rtbInput.Text
        Clear()
        CheckError()
    End Sub

    Private Sub btnIgnore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIgnore.Click
        Dim pos As Integer = 0
        Dim searchtext As String = temGrammarError
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
        Loop
        ignorelist.Add(temGrammarError)
        CheckError()
    End Sub

    Private Sub clear()
        Me.rtbInput.SelectAll()
        Me.rtbInput.SelectionFont = myfont1
        Me.rtbInput.SelectionColor = Color.Black
        Me.rtbInput.Select(0, 0)
    End Sub


    'Private Sub CheckError2()
    '    tList = mygrammar.returnlist
    '    lstSuggestion.Items.Clear()

    '    Dim pos As Integer = 0
    '    If ignorelist.Contains(item.Key) = False Then
    '        Dim searchtext As String = item.Key
    '        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
    '        Do While pos <> -1
    '            Dim myfont As New Font("Faruma", 11, FontStyle.Bold)
    '            If rtbInput.SelectedText = searchtext Then
    '                temGrammarError = searchtext
    '                rtbInput.SelectionLength = searchtext.Length
    '                rtbInput.SelectionColor = Color.Green
    '                rtbInput.SelectionUnderlineStyle = ExtendedRichTextBox.UnderlineStyle.HeavyWave
    '                Dim mystring As String() = item.Value.Split(":")
    '                lstSuggestion.Items.Add(mystring(0))
    '                txtExplain.Text = mystring(1)
    '                lstSuggestion.SelectedIndex = 0
    '            End If
    '            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
    '        Loop
    '    End If
    'End Sub

#Region "Form Move/Drag"

    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Private Sub frmGrammar_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctback.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub

    Private Sub frmGrammar_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctback.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub frmGrammar_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pctback.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub
#End Region

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        frmMain.rtbInput.Text = Me.rtbInput.Text
        Me.Close()
    End Sub
End Class